package com.sirdc.modules.sys.web.index;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.util.GetIpAddress;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.entity.IndicatorBreakdown;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.service.CompletionClassificationService;
import net.huashitong.Indicators.service.IndicatorBreakdownService;
import net.huashitong.Indicators.utils.DataUtils;
import net.huashitong.Indicators.utils.DateForIndUtils;
import net.huashitong.Indicators.utils.IndustryNameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

@Controller
@RequestMapping("/index/indicatorBreakdown/")
public class IndexIndicatorBreakdownAppController extends JsonBaseController {
    @Autowired
    private IndicatorBreakdownService indicatorBreakdownService;
    @Autowired
    private CompletionClassificationService completionClassificationService;

    @Override
    protected String getView(String arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 排名页接口
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryRank")
    public Message queryRank(@RequestBody IndicatorBreakdownFilter filter) {
        //最新的时间
        String newDate = indicatorBreakdownService.getNewDate();
        filter.setDate(DateForIndUtils.getMonth(newDate));
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        String[] industryNames = {
                "地区生产总值", "第一产业增加值", "第二产业增加值", "第三产业增加值", "建筑业增加值", "社会消费品零售总额", "规模以上工业增加值", "固定资产投资", "工业固投", "一般公共预算总收入", "一般公共预算收入", "进出口总值", "实际利用外资"/*,"农林牧渔业总产值"*/, "城镇居民人均可支配收入", "农村居民人均可支配收入"
        };
        for (int i = 0; i < industryNames.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            List breakdown = indicatorBreakdownService.queryAll(filter.getDate(), industryNames[i], filter.getArea());
            if(ObjectUtils.isBlank(breakdown)){
                IndicatorBreakdown ib = indicatorBreakdownService.getIndicatorDateTimeByName(industryNames[i]);
                breakdown = indicatorBreakdownService.queryAll(ib.getDate(), industryNames[i], filter.getArea());
            }

            String mData = "01";
            String siz = "0";
            if (ObjectUtils.isBlank(breakdown)) {
                if ("1".equals(filter.getState())) {
                    siz = "1";
                    filter.setIndustryName(industryNames[i]);
                    filter = IndustryNameUtils.changeId(filter);
                    String judge = DataUtils.getValue(filter);
                    String date = DataUtils.getValueMonth(judge, filter);
                    breakdown = indicatorBreakdownService.queryAll(DateForIndUtils.getMonth(date), industryNames[i], filter.getArea());
                    mData = date.substring(date.length() - 2, date.length());
                } else {
                    continue;
                }
            }
            Object[] object = (Object[]) breakdown.get(0);
            Double double1 = Double.parseDouble(object[1].toString());
            Double double2 = Double.parseDouble(object[5].toString());
            BigDecimal data1 = new BigDecimal(double1);
            BigDecimal data2 = new BigDecimal(double2);
            int k = data1.compareTo(data2);
            if (k < 0) {
                map.put("reach", "0");
            } else {
                if ("1".equals(map.get("rank")) || "2".equals(map.get("rank")) || "3".equals(map.get("rank"))) {
                    map.put("reach", "2");
                } else {
                    map.put("reach", "1");
                }
            }

            map.put("industryId", object[0]);
            map.put("ratio", object[1]);
            if ("1".equals(siz) && "1".equals(filter.getState())) {
                if ("03".equals(mData)) {
                    map.put("industryName", object[2] + "(第一季度)");
                } else if ("06".equals(mData)) {
                    map.put("industryName", object[2] + "(第二季度)");
                } else if ("09".equals(mData)) {
                    map.put("industryName", object[2] + "(第三季度)");
                } else if ("12".equals(mData)) {
                    map.put("industryName", object[2] + "(第四季度)");
                }

            } else {
                map.put("industryName", object[2]);
            }
            map.put("cumulative", object[3]);

            if ("规模以上工业增加值".equals(industryNames[i]) && "201701-11".equals(filter.getDate())) {
                map.put("rank", "5");
            } else if ("规模以上工业增加值".equals(industryNames[i]) && "201801-02".equals(filter.getDate())) {
                map.put("rank", "11");
            }

            if ("社会消费品零售总额".equals(industryNames[i]) && "201701-12".equals(filter.getDate())) {
                map.put("rank", "5");
            } else if ("社会消费品零售总额".equals(industryNames[i]) && "201801-02".equals(filter.getDate())) {
                map.put("rank", "6");
            } else {
                map.put("rank", object[4] == null ? "-" : object[4].toString());
            }

            if ("第一产业增加值".equals(industryNames[i]) && "201701-12".equals(filter.getDate())) {
                map.put("rank", "1");
            }

            if ("城镇居民人均可支配收入".equals(industryNames[i]) || "农村居民人均可支配收入".equals(industryNames[i])) {
                map.put("unit", "元");
            } else {
                map.put("unit", object[6]);
            }
            if ("规模以上工业产值".equals(industryNames[i])) {
                map.put("rank", "-");
            }
			/* if("01".equals(mData)&&!"进出口总值".equals(industryNames[i])){
				 map.put("rank", "0");
			 }*/
            if ("固定资产投资".equals(industryNames[i]) && "201801-02".equals(filter.getDate())) {
                map.put("rank", "2");
            }
            if ("一般公共预算总收入".equals(industryNames[i]) && "201801-02".equals(filter.getDate())) {
                map.put("rank", "2");
            }
            if ("一般公共预算收入".equals(industryNames[i]) && "201801-02".equals(filter.getDate())) {
                map.put("rank", "4");
            }
            if ("规模以上工业增加值".equals(industryNames[i]) && "201801-02".equals(filter.getDate())) {
                map.put("rank", "11");
            }

            if ("固定资产投资".equals(industryNames[i]) && "201801-03".equals(filter.getDate())) {
                map.put("rank", "2");
            }
            if ("工业固投".equals(industryNames[i]) && "201801-03".equals(filter.getDate())) {
                map.put("rank", "9");
            }
            if ("实际利用外资".equals(industryNames[i]) && "201801-03".equals(filter.getDate())) {
                map.put("rank", "-");
            }
            if ("规模以上工业增加值".equals(industryNames[i]) && "201801-05".equals(filter.getDate())) {
                map.put("rank", "6");
            }
            list.add(map);
        }
        if (ObjectUtils.isBlank(list)) {
            return coverMessage("500", "暂无数据");
        }
        for (Map<String, Object> map : list) {
            map = changeName(map);
            if ("亿元".equals(map.get("unit"))) {
                String val = map.get("cumulative").toString();

                Double double1 = Double.parseDouble(val);
//                double1 = double1/10000;
                String st = "";
                if (double1 < 1) {
                    if(double1 == 0.0 ){
                        map.put("cumulative", "-");
                    }else{
                        map.put("cumulative", double1.toString() + "万元");
                    }

                } else {
                    DecimalFormat df = new DecimalFormat("#.##");
                    st = df.format(double1);
                    map.put("cumulative", st + "万元");
                }
            } else {
                String val = map.get("cumulative").toString();
                Double double1 = Double.parseDouble(val);
                String st = "";
                DecimalFormat df = new DecimalFormat("#.##");
                st = df.format(double1);
                map.put("cumulative", st + map.get("unit"));
            }
            Double double2 = Double.parseDouble(map.get("ratio").toString());
            if (double2 > 1) {
                DecimalFormat df = new DecimalFormat("#.##");
                String st = df.format(double2);
                map.put("ratio", st);
            } else {
                DecimalFormat df = new DecimalFormat("#.##");
                String st = df.format(double2);
                map.put("ratio", st);
            }

        }
        return coverMessage("200", "数据获取成功", list);
        /*List<Map<String, String>> result = new ArrayList<>();
        for (int i = 0; i < industryNames.length; i++) {
            Map<String, String> map = new HashMap<>();
            List breakdown = indicatorBreakdownService.queryAll(filter.getDate(), industryNames[i], filter.getArea());
            Object[] object = (Object[]) breakdown.get(0);

            String industryId = object[0].toString();
            String ratio = object[1].toString();
            String industryName = object[2].toString();
            String cumulative = object[3].toString();
            String rank = object[4].toString();
            String countyIncrease = object[5].toString();
            String unit = object[6].toString();
            map.put("industryId", industryId);
            map.put("ratio", ratio);
            map.put("industryName", industryName);
            map.put("cumulative", "0".equals(cumulative) ? "-" : cumulative+object[6].toString());
            map.put("rank", rank);
            map.put("countyIncrease", countyIncrease);
            map.put("unit", unit);
            result.add(map);
        }
        Collections.sort(result, new Comparator<Map<String, String>>() {
            @Override
            public int compare(Map<String, String> o1, Map<String, String> o2) {
                if (StringUtils.isNotBlank(o1.get("ratio")) && StringUtils.isNotBlank(o2.get("ratio"))) {
                    if (Double.parseDouble(o1.get("ratio")) < Double.parseDouble(o2.get("ratio"))) {
                        return 1;
                    } else if (Double.parseDouble(o1.get("ratio")) == Double.parseDouble(o2.get("ratio"))) {
                        return 0;
                    } else {
                        return -1;
                    }
                }
                return 0;
            }
        });
        for (int i = 0; i < result.size(); i++) {
            result.get(i).put("rank", i + 1 + "");
        }
        return coverMessage("200", "数据获取成功", result);*/
    }


    public Map<String, Object> changeName(Map<String, Object> map) {

        if (map.get("industryName").toString().contains("农村")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "农村人均收入" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "农村人均收入");
            }

        }
        if (map.get("industryName").toString().contains("公共预算总收入")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "公共预算总收入" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "公共预算总收入");
            }

        }
        if (map.get("industryName").toString().contains("公共预算收入")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "公共预算收入" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "公共预算收入");
            }
        }
        if (map.get("industryName").toString().contains("第一产业")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "第一产业" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "第一产业");
            }
        }
        if (map.get("industryName").toString().contains("第二产业")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "第二产业" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "第二产业");
            }
        }
        if (map.get("industryName").toString().contains("第三产业")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "第三产业" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "第三产业");
            }
        }
        if (map.get("industryName").toString().contains("农林牧渔业")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "农林牧渔业" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "农林牧渔业");
            }
        }
        if (map.get("industryName").toString().contains("城镇")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "城镇人均收入" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "城镇人均收入");
            }

        }
        if (map.get("industryName").toString().contains("规模以上工业增加值")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "规模以上工业" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "规模以上工业完成情况");
            }

        }
        if (map.get("industryName").toString().contains("建筑业")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "建筑业" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "建筑业");
            }
        }
        if (map.get("industryName").toString().contains("地区生产总值")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "GDP" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "GDP");
            }

        }
        if (map.get("industryName").toString().contains("社会消费品零售总")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "全社会消费品零售总额 " + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "全社会消费品零售总额 ");
            }
        }
        if (map.get("industryName").toString().contains("实际利用")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "实际外资" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "实际外资");
            }

        }
        return map;
    }

    /**
     * 返回有数据的月份
     *
     * @param filter
     * @return
     */
    @RequestMapping("/queryHaveValue")
    @ResponseBody
    public Message queryHaveValue(@RequestBody IndicatorBreakdownFilter filter) {
        String judge = DataUtils.getValue(filter);
        Map<String, Object> map = new HashMap<String, Object>();
        String date = DataUtils.getValueMonth(judge, filter);
        map.put("judge", judge);
        map.put("date", date);
        return coverMessage("200", "获取成功", map);
    }


    /**
     * 排名页接口
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryIndustryNameList")
    public Message queryIndustryNameList(@RequestBody IndicatorBreakdownFilter filter , HttpServletRequest request) {

        String clientAddress = GetIpAddress.getClientAddress(request);
        System.out.println("------------请求Ip："+clientAddress+"------------");

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        String[] industryNames = {
                "地区生产总值", "建筑业增加值", "规模以上工业增加值", "固定资产投资", "社会消费品零售总额", "一般公共预算总收入", "一般公共预算收入", "工业固投", "进出口总值", "实际利用外资", "第一产业增加值", "第三产业增加值"
        };
        String month = DateForIndUtils.getMonth(filter.getDate());
        for (int i = 0; i < industryNames.length; i++) {
            filter.setDate(DateForIndUtils.getMonth(month));
            Map<String, Object> map = new HashMap<String, Object>();
            if ("地区生产总值".equals(industryNames[i])) {
                map.put("url", "/index/gdp");
            } else if ("建筑业增加值".equals(industryNames[i])) {
                map.put("url", "/index/buildingIndustry");
            } else if ("规模以上工业增加值".equals(industryNames[i])) {
                map.put("url", "/index/industrialScale");
            } else if ("固定资产投资".equals(industryNames[i])) {
                map.put("url", "/index/fixedAssets");
            } else if ("社会消费品零售总额".equals(industryNames[i])) {
                map.put("url", "/index/socialSpending");
            } else if ("一般公共预算总收入".equals(industryNames[i])) {
                map.put("url", "/index/budget1");
            } else if ("一般公共预算收入".equals(industryNames[i])) {
                map.put("url", "/index/budget2");
            } else if ("工业固投".equals(industryNames[i])) {
                map.put("url", "/index/industrialInvestment");
            } else if ("进出口总值".equals(industryNames[i])) {
                map.put("url", "/index/exports");
            } else if ("实际利用外资".equals(industryNames[i])) {
                map.put("url", "/index/actualForeign");
            } else if ("第一产业增加值".equals(industryNames[i])) {
                map.put("url", "/index/oneProduction");
            } else if ("第二产业增加值".equals(industryNames[i])) {
                map.put("url", "/index/twoProduction");
            } else if ("第三产业增加值".equals(industryNames[i])) {
                map.put("url", "/index/threeProduction");
            }
            List breakdown = indicatorBreakdownService.queryAll(filter.getDate(), industryNames[i], filter.getArea());
            for (Object o : breakdown) {
                Object[] object = (Object[]) o;
                Object o1 = object[0];
                Object o2 = object[2];
                System.out.println("************指标："+o2+"id"+o1+"*************");
            }
            String mData = "01";
            String siz = "0";
            if (ObjectUtils.isBlank(breakdown)) {
                if ("1".equals(filter.getState())) {
                    siz = "1";
                    filter.setIndustryName(industryNames[i]);
                    filter = IndustryNameUtils.changeId(filter);
                    String judge = DataUtils.getValue(filter);
                    String date = DataUtils.getValueMonth(judge, filter);
                    System.out.println("————————指标："+industryNames[i]+"-----------------date: "+date+"---------");
                    breakdown = indicatorBreakdownService.queryAll(DateForIndUtils.getMonth(date), industryNames[i], filter.getArea());
                    mData = date.substring(date.length() - 2, date.length());
                } else {
                    continue;
                }
            }
            Object[] object = (Object[]) breakdown.get(0);
            Double double1 = Double.parseDouble(ObjectUtils.isBlank(object[1].toString())?"0":object[1].toString());
            Double double2 = Double.parseDouble(object[5].toString());
            BigDecimal data1 = new BigDecimal(double1);
            BigDecimal data2 = new BigDecimal(double2);
            int k = data1.compareTo(data2);
            if (k < 0) {
                map.put("reach", "0");
            } else {
                if ("1".equals(map.get("rank")) || "2".equals(map.get("rank")) || "3".equals(map.get("rank"))) {
                    map.put("reach", "2");
                } else {
                    map.put("reach", "1");
                }
            }

            map.put("industryId", object[0]);
            map.put("ratio", object[1]);
            if ("1".equals(siz) && "1".equals(filter.getState())) {
                if ("03".equals(mData)) {
                    map.put("industryName", object[2] + "");
                } else if ("06".equals(mData)) {
                    map.put("industryName", object[2] + "");
                } else if ("09".equals(mData)) {
                    map.put("industryName", object[2] + "");
                } else if ("12".equals(mData)) {
                    map.put("industryName", object[2] + "");
                }

            } else {
                map.put("industryName", object[2]);
            }
            map.put("cumulative", object[3]);

            if ("规模以上工业增加值".equals(industryNames[i]) && "201701-11".equals(filter.getDate())) {
                map.put("rank", "5");
            } else if ("规模以上工业增加值".equals(industryNames[i]) && "201801-02".equals(filter.getDate())) {
                map.put("rank", "11");
            }

            if ("社会消费品零售总额".equals(industryNames[i]) && "201701-12".equals(filter.getDate())) {
                map.put("rank", "5");
            } else if ("社会消费品零售总额".equals(industryNames[i]) && "201801-02".equals(filter.getDate())) {
                map.put("rank", "6");
            } else {
                map.put("rank", object[4] == null ? "-" : object[4].toString());
            }

            if ("第一产业增加值".equals(industryNames[i]) && "201701-12".equals(filter.getDate())) {
                map.put("rank", "1");
            }

            if ("城镇居民人均可支配收入".equals(industryNames[i]) || "农村居民人均可支配收入".equals(industryNames[i])) {
                map.put("unit", "元");
            } else {
                map.put("unit", object[6]);
            }
            if ("规模以上工业产值".equals(industryNames[i])) {
                map.put("rank", "-");
            }
			/* if("01".equals(mData)&&!"进出口总值".equals(industryNames[i])){
				 map.put("rank", "0");
			 }*/
            if ("固定资产投资".equals(industryNames[i]) && "201801-02".equals(filter.getDate())) {
                map.put("rank", "2");
            }
            if ("一般公共预算总收入".equals(industryNames[i]) && "201801-02".equals(filter.getDate())) {
                map.put("rank", "2");
            }
            if ("一般公共预算收入".equals(industryNames[i]) && "201801-02".equals(filter.getDate())) {
                map.put("rank", "4");
            }
            if ("规模以上工业增加值".equals(industryNames[i]) && "201801-02".equals(filter.getDate())) {
                map.put("rank", "11");
            }

            if ("固定资产投资".equals(industryNames[i]) && "201801-03".equals(filter.getDate())) {
                map.put("rank", "2");
            }
            if ("工业固投".equals(industryNames[i]) && "201801-03".equals(filter.getDate())) {
                map.put("rank", "9");
            }
            if ("实际利用外资".equals(industryNames[i]) && "201801-03".equals(filter.getDate())) {
                map.put("rank", "-");
            }
            if ("规模以上工业增加值".equals(industryNames[i]) && "201801-05".equals(filter.getDate())) {
                map.put("rank", "6");
            }
            list.add(map);
        }
        if (ObjectUtils.isBlank(list)) {
            return coverMessage("500", "暂无数据");
        }
        for (Map<String, Object> map : list) {
            map = changeName(map);
            if ("亿元".equals(map.get("unit"))) {
                String val = map.get("cumulative").toString();

                Double double1 = Double.parseDouble(val);
                double1 = double1 / 10000;
                String st = "";
                if (double1 < 1) {
                    map.put("cumulative", double1.toString() + map.get("unit"));
                } else {
                    DecimalFormat df = new DecimalFormat("  #.##");
                    st = df.format(double1);
                    map.put("cumulative", st + map.get("unit"));
                }
            } else {
                String val = map.get("cumulative").toString();
                Double double1 = Double.parseDouble(val);
                String st = "";
                DecimalFormat df = new DecimalFormat("#.##");
                st = df.format(double1);
                map.put("cumulative", st + map.get("unit"));
            }
            String ratio = "0";
            if(!ObjectUtils.isBlank(map.get("ratio"))){
                ratio = map.get("ratio").toString();
            }
            Double double2 = Double.parseDouble(ratio);
            if (double2 > 1) {
                DecimalFormat df = new DecimalFormat("#.##");
                String st = df.format(double2);
                map.put("ratio", st);
            } else {
                DecimalFormat df = new DecimalFormat("#.##");
                String st = df.format(double2);
                map.put("ratio", st);
            }

        }
        return coverMessage("200", "数据获取成功", list);
    }


}
